package com.cangxuan.app.ui.user;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.alibaba.fastjson.JSONObject;
import com.cangxuan.app.R;

import com.cangxuan.app.entity.User;
import com.cangxuan.app.ui.BaseActivity;
import com.cangxuan.app.utils.DeviceUtils;
import com.cangxuan.app.utils.HttpUtils;
import com.cangxuan.app.utils.UserUtils;

import java.net.HttpURLConnection;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: 田凯
 * Activity: 消息通知设置
 */
public class SetNoticeActivity extends BaseActivity  {

    ConstraintLayout newMessage;
    Switch calendarSwitch;
    Handler handler =new Handler();
    LinearLayout dataLoading;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_notice);
        setTitle("消息通知设置");
        newMessage=findViewById(R.id.newMessage);
        calendarSwitch=findViewById(R.id.calendarSwitch);
        dataLoading=findViewById(R.id.dataLoading);

        User user=UserUtils.getUser(this);
        calendarSwitch.setChecked(user.isSubscribeToTheSalesCalendar());

        Map<String,String> header=new HashMap<>();
        //获取设备欸id  ffffffff-b61d-886d-0000-00005c13d641
        header.put("Authorization", DeviceUtils.getDeviceID());
//        Log.i("header:",header+"");
        //
        newMessage.setOnClickListener(view->{
            Log.i("新消息通知", "新消息通知被点击了\t" + LocalDateTime.now());

        });

        //switch事件绑定
        calendarSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
            ProgressDialog progressDialog = ProgressDialog.show(this, "", "请稍等...");
            if(b){
                //调用API接口获取数据，
                Thread thread=new Thread(() -> {
                    String post= HttpUtils.post("/api/user/changeSubscribeToTheSalesCalendar",header,"true") ;
                    JSONObject jsonResponse = JSONObject.parseObject(post);
                    if(jsonResponse.getInteger("status") == HttpURLConnection.HTTP_OK){
                        handler.post(()->{
//                        calendarSwitch.setChecked(true);
                            progressDialog.dismiss();
                            Toast.makeText(this, jsonResponse.getString("message"), Toast.LENGTH_SHORT).show();
                            user.setSubscribeToTheSalesCalendar(true);
                            UserUtils.setUser(this,user);
                        });

                    }

                });
                thread.start();

            }else {
                calendarSwitch.setChecked(false);
                //调用API接口获取数据，
                Thread thread=new Thread(() -> {
                    String post= HttpUtils.post("/api/user/changeSubscribeToTheSalesCalendar",header,"false") ;
                    JSONObject jsonResponse = JSONObject.parseObject(post);
                    if(jsonResponse.getInteger("status") == HttpURLConnection.HTTP_OK) {
                        handler.post(() -> {
                            progressDialog.dismiss();
                            Toast.makeText(this, jsonResponse.getString("message"), Toast.LENGTH_SHORT).show();
                            user.setSubscribeToTheSalesCalendar(false);
                            UserUtils.setUser(this, user);

                        });
                    }
                });
                thread.start();
                Log.i("订阅发售日历","取消订阅发售日历\t" + LocalDateTime.now());
            }

        });


    }

}
